﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Digitalmes.ControlEx;

/// <summary>
/// LED 闪烁控件
/// </summary>
public partial class Led : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    /// <summary>
    /// 是否鼠标正处于按下状态
    /// </summary>
    public bool IsPreviewMouseDown {  get; set; }

    /// <summary>
    /// 控件当前鼠标按下的位置
    /// </summary>
    public Point MouseDownPosition { get; set; }

    /// <summary>
    /// 控件当前在 Canvas 中的位置
    /// </summary>
    public Point CanvasPosition { get; set; }

    public Led()
    {
        InitializeComponent();
        DataContext = this;
    }

    private int _diameter;
    /// <summary>
    /// 圆直径
    /// </summary>
    [Bindable(true), Category("属性")]
    public int Diameter
    {
        get { return _diameter; }
        set { _diameter = value; OnPropertyChanged(nameof(Diameter)); }
    }

    private string? _text;
    /// <summary>
    /// 显示文本
    /// </summary>
    [Bindable(true), Category("属性")]
    public string? Text
    {
        get { return _text; }
        set { _text = value; OnPropertyChanged(nameof(Text)); }
    }

    private string? _textColor = "Black";
    /// <summary>
    /// 显示文本颜色（默认 Black）
    /// </summary>
    [Bindable(true), Category("属性")]
    public string? TextColor
    {
        get { return _textColor; }
        set { _textColor = value; OnPropertyChanged(nameof(TextColor)); }
    }

    private double _textFontSize = 14;
    /// <summary>
    /// 显示文本字体大小（默认 14）
    /// </summary>
    [Bindable(true), Category("属性")]
    public double TextFontSize
    {
        get { return _textFontSize; }
        set { _textFontSize = value; OnPropertyChanged(nameof(TextFontSize)); }
    }

    private int _index;
    /// <summary>
    /// 索引
    /// </summary>
    [Bindable(true), Category("属性")]
    public int Index
    {
        get { return _index; }
        set { _index = value; OnPropertyChanged(nameof(Index)); }
    }

    private string _group = string.Empty;
    /// <summary>
    /// 索引分组
    /// </summary>
    [Bindable(true), Category("属性")]
    public string Group
    {
        get { return _group; }
        set { _group = value; OnPropertyChanged(nameof(Group)); }
    }

    private LedState _state = LedState.None;
    /// <summary>
    /// 状态
    /// </summary>
    [Bindable(true), Category("属性")]
    public LedState State
    {
        get { return _state; }
        set { _state = value; OnPropertyChanged(nameof(State)); }
    }

    private string _ledStateNoneColor = "LightGray";
    /// <summary>
    /// <see cref="LedState.None"/> 状态颜色（默认 LightGray）
    /// </summary>
    [Bindable(true), Category("属性")]
    public string LedStateNoneColor
    {
        get { return _ledStateNoneColor; }
        set { _ledStateNoneColor = value; OnPropertyChanged(nameof(LedStateNoneColor)); }
    }

    private string _ledStateOkColor = "LimeGreen";
    /// <summary>
    /// <see cref="LedState.OK"/> 状态颜色（默认 LimeGreen）
    /// </summary>
    [Bindable(true), Category("属性")]
    public string LedStateOkColor
    {
        get { return _ledStateOkColor; }
        set { _ledStateOkColor = value; OnPropertyChanged(nameof(LedStateOkColor)); }
    }

    private string _ledStateNgColor = "Red";
    /// <summary>
    /// <see cref="LedState.NG"/> 状态颜色（默认 Red）
    /// </summary>
    [Bindable(true), Category("属性")]
    public string LedStateNgColor
    {
        get { return _ledStateNgColor; }
        set { _ledStateNgColor = value; OnPropertyChanged(nameof(LedStateNgColor)); }
    }

    protected internal virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
